package com.linjm.util;


import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class FileUploadUtil {
    private String fileUrl="http://192.168.1.10";
    //localhost:8082/fileUpload
    @RequestMapping("fileUpload")
    public String fileUpload(MultipartFile file) throws IOException, MyException {
        if (file != null) {
            String configfile = this.getClass().getResource("/tracker.conf").getFile();
            ClientGlobal.init(configfile);
            TrackerClient trackerClient = new TrackerClient();
            //获取链接
            TrackerServer trackerServer = trackerClient.getTrackerServer();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //获取上传的文件名称
            String orginalFilename = file.getOriginalFilename();
            //获取文件的后缀名
            String extName = StringUtils.substringAfterLast(orginalFilename, ".");
            //上传图片
            String[] upload_file = storageClient.upload_file (file.getBytes(), extName, null);
            for (int i = 0; i < upload_file.length; i++) {
                String path = upload_file[i];
                fileUrl+="/"+path;
            }
        }
        return fileUrl;
    }
}
